package com.lx.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lx.bean.Result;
import com.lx.biz.BizException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;

public class Utils {


    public static void WriteResult(int code, String msg, Object object, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        Result result = new Result(code, msg, object);
        String json = new ObjectMapper().writeValueAsString(result);
        response.getWriter().append(json);
    }

    public static void check(boolean b, String msg) throws BizException {
        if (b) throw new BizException(msg);
    }

    public static void checkEmpty(Object value, String msg) throws BizException {
        if (value == null) throw new BizException(msg);
        if (value instanceof String) {
            check(((String) value).isBlank(), msg);
        }

        if (value instanceof Collection) {
            check(((Collection) value).isEmpty(), msg);
        }

        if (value instanceof Map) {
            check(((Map) value).isEmpty(), msg);
        }
    }
}
